<template>
	<view class="wrap">
		<u-loading-page :loading="loading"></u-loading-page>
		<view class="content" v-if="!loading">
			<view class="item">
				<view class="title">二维码管理</view>
				<view class="info">
					<view class="flex">
						<image class="img" :src="inviteCode" mode=""></image>
					</view>
					<view class="btn-wrap">
						<view class="save btn" @click="savePhoto(inviteCode)">
							<u-icon name="photo" color="#fff"></u-icon>
							<text class="text">保存二维码</text>
						</view>
					</view>
				</view>
			</view>
			<view class="item" v-if="menu.exemptType == 1">
				<view class="title">免审二维码</view>
				<view class="info">
					<view class="flex">
						<image class="img" :src="avoidCode" mode=""></image>
					</view>
					<view class="btn-wrap">
						<view class="reload btn" @click="getDriverInviteTrialExemption">
							<u-icon name="reload"></u-icon>
							<text class="text">重制二维码</text>
						</view>
						<view class="save btn" style="margin-left: 64rpx;" @click="savePhoto(avoidCode)">
							<u-icon name="photo" color="#fff"></u-icon>
							<text class="text">保存二维码</text>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import Cache from '@/utils/cache'
	import {
		driverInvite,
		driverInviteTrialExemption
	} from '@/config/api/app.js'
	export default {
		data() {
			return {
				inviteCode: '',
				avoidCode: '',
				loading: true,
				menu: Cache.get('MENU')
			}
		},
		async onLoad() {
			console.log(this.menu)
			await this.getDriverInvite()
			this.menu.exemptType == 1 && await this.getDriverInviteTrialExemption()
			setTimeout(() => {
				this.loading = false
			}, 600)
		},
		methods: {
			getDriverInvite() {
				driverInvite().then(res => {
					if (res.code == 0) {
						this.inviteCode = res.data
					} else {
						uni.$u.toast(res.msg);
					}
				}).catch(err => {
					console.log(err)
				})
			},
			getDriverInviteTrialExemption() {
				driverInviteTrialExemption().then(res => {
					if (res.code == 0) {
						this.avoidCode = res.data
					} else {
						uni.$u.toast(res.msg);
					}
				}).catch(err => {
					console.log(err)
				})
			},
			savePhoto(src) {
				// #ifdef MP-WEIXIN

				// #endif
				// #ifdef APP-PLUS
				
				// #endif
				uni.downloadFile({
					url: src,
					success: (res) => {
						console.log(res.tempFilePath)
						//临时文件路径
						let imageUrl = res.tempFilePath;
						//保存图片到系统相册
						uni.saveImageToPhotosAlbum({
							filePath: imageUrl,
							success: (res) => {
								uni.showToast({
									title: "二维码保存成功",
									icon: "success"
								})
							},
							fail: (err) => {
								uni.showToast({
									title: "二维码保存失败",
									icon: "error"
								})
							}
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #FAFAFA;
	}

	.item {
		margin-top: 16rpx;
		background: #fff;
		padding: 32rpx 0 48rpx;

		.title {
			font-size: 34rpx;
			font-weight: 500;
			color: #333333;
			padding-left: 24rpx;
			border-left: 8rpx solid #003E81;
			box-sizing: border-box;
		}

		.info {
			margin-top: 48rpx;
			display: flex;
			flex-direction: column;
			justify-content: center;
			padding: 0 80rpx;

			.flex {
				display: flex;
				align-items: center;
				justify-content: center;
				margin-bottom: 48rpx;
			}

			.img {
				width: 380rpx;
				height: 380rpx;
			}

			.btn-wrap {
				display: flex;
				align-items: center;
				justify-content: center;

				.btn {
					display: flex;
					align-items: center;
					justify-content: center;
					font-size: 26rpx;
					font-weight: 500;
					padding: 16rpx 24rpx;

					.text {
						margin-left: 12rpx;
					}
				}

				.save {
					color: #FFFFFF;
					background: #003E81;
					border-radius: 6rpx;
					border: 1rpx solid #003E81;
				}

				.reload {
					color: #333333;
					background: #fff;
					border-radius: 6rpx;
					border: 1rpx dashed #E8E8E8;
				}
			}
		}
	}
</style>
